<style scoped>
  .weui-form-preview__hd label, .weui-form-preview__hd em {
    font-size: .9em !important;
  }
  .weui-loadmore_line .weui-loadmore__tips {
    background-color: #eee;
  }
  .rj-cell.max span{
    font-size: 16px;
    padding-right: 20px;
  }
  .rj-cell label {
    color: #333;
    font-size: 14px;
    width: 44%;
    display: inline-block;
    padding: 5px 3%;
    text-align: right;
  }
  .rj-cell span {
    font-size: 14px;
    color: #333;
    display: inline-block;
    width: 50%;
  }
  .rj-cell.note label{
    float: left;
  }
  .rj-cell.note span{
    width: 80%;
    display: inline-block;
  }
  .weui-cell {
    background: #fff;
  }
  #acts span {
    font-size: 14px;
  }
  .halfbtn {
    display: inline-block;
    width: 49%;
  }
  .weui-label{
    /*width: 130px;*/
    text-align: left;
  }

  .weui-select {
    height: auto;
    line-height: normal;
  }
  *{
    list-style:none;
  }
  /*body{*/

  /*}*/
  .weui-cell_select {
    padding: 10px 15px;
  }
  .weui-cells__title {
    text-align: left;
  }
  .hide {
    display: none;
  }
  #acts {
    font-size: 0;
  }
  #acts span {
    font-size: 14px;
  }
  input.time {
    border: none;
    font-size: 16px;
    padding-left: 10px;
  }
</style>
<template>
  <div class="oitem">
    <div class="weui-cells__title">货品明细{{itemNo + 1}}
      <p id="acts" style="float: right;color: #1aad19;" v-if="isLast">
        <span id="addItem" @click="addItem" style="padding-right: 5px;">添加</span>
        <span v-if="itemNo != 0">|</span>
        <span id="delItem" style="padding-left: 5px;" @click="delItem" v-if="itemNo != 0">删除</span>
      </p>
    </div>
    <div class="weui-cell weui-cell_select fir">
      <div class="weui-cell__hd"><label class="weui-label">品种</label></div>
      <div class="weui-cell__bd">
        <input type="hidden" ref="productname" :name="'pitems[' + itemNo + '].productname'">
        <select class="weui-select product" :name="'pitems[' + itemNo + '].productguid'" :id="'product' + itemNo" v-model="productid">
          <option value="" disabled>请选择</option>
          <option v-for="(item, index) in products" :value="item.productguid">{{item.productname}}</option>
        </select>
      </div>
    </div>
    <div class="weui-cell weui-cell_select">
      <div class="weui-cell__hd"><label class="weui-label">规格</label></div>
      <div class="weui-cell__bd">
        <input type="hidden" ref="spname" :name="'pitems[' + itemNo + '].spname'">
        <select class="weui-select specif" :name="'pitems[' + itemNo + '].spguid'" :id="'spec' + itemNo" v-model="specid">
          <option value="" disabled>请选择</option>
          <option   v-for="(item, index) in specs" :value="item.spguid">{{item.spname}}</option>
        </select>
      </div>
    </div>
    <div class="weui-cell" v-if="settletype == 1 && benifitordeduct == 1 && (benefittype == 3 && contract != '')">
      <div class="weui-cell__hd"><label class="weui-label">赠送(%)</label></div>
      <div class="weui-cell__bd">
        <input class="weui-input" type="text" :name="'pitems[' + itemNo + '].deductrate'" readonly :value="prices.concession">
      </div>
    </div>
    <div class="weui-cell" v-if="settletype == 1">
      <div class="weui-cell__hd"><label class="weui-label">订单件数</label></div>
      <div class="weui-cell__bd">
        <input class="weui-input count" type="number" :name="'pitems[' + itemNo + '].orderpack'" placeholder="请输入订单件数">
      </div>
    </div>
    <div class="weui-cell" v-if="benifitordeduct == 1 && settletype == 1 && benefittype == 5">
      <div class="weui-cell__hd"><label class="weui-label">赠送件数</label></div>
      <div class="weui-cell__bd">
        <input class="weui-input" type="number" v-model="handselcount" :name="'pitems[' + itemNo + '].handselcount'" placeholder="请输入赠送件数">
      </div>
    </div>
    <div class="weui-cell" v-if="settletype == 2">
      <div class="weui-cell__hd"><label class="weui-label">订单重量</label></div>
      <div class="weui-cell__bd">
        <input class="weui-input weight inlinput1" v-model="weight" type="number" :name="'pitems[' + itemNo + '].weight'" placeholder="请输入订单重量">
        <input class="weui-input inlinput2" type="text" readonly value="吨">
      </div>
    </div>
<!--    <div class="weui-cell" v-if="benifitordeduct == 1 && settletype == 2 && benefittype == 5">-->
<!--      <div class="weui-cell__hd"><label class="weui-label">赠送重量</label></div>-->
<!--      <div class="weui-cell__bd">-->
<!--        <input class="weui-input" type="number" v-model="handselweight" :name="'pitems[' + itemNo + '].handselweight'" placeholder="请输入赠送重量">-->
<!--      </div>-->
<!--    </div>-->
  </div>
</template>

<script>
export default {
  name: 'DispatchCustomItem',
  data () {
    return {
      products: [],
       productid: '',
      specs: [],
      specid: '',
      weight: '',
      currProduct: '',
      prices: '',
      itemSettledWeight: 0,
      itemSettledCount: 0,
      handselcount: '',
      handselweight: '',
    }
  },
  props: [
    'sellformid',
    'itemNo',
    'isLast',
    'settletype',
    'contract',
    'quitsrtype',
    'benifitordeduct',
    'benefittype',
    'discount',
    'currency',
    'items'

  ],
  mounted() {
    this.getProducts();
  },
  watch: {
    'sellformid': function (val) {
      this.getProducts();
    },
    'productid': function (val) {
      this.getSpecif(val);
      // this.$refs.productname.value = $('#product').find('option:selected').text();
      this.$refs.productname.value = $('#product' + this.itemNo).find('option:selected').text();
      // if (this.contract != '') {
      //   this.currProduct = this.contract.items.filter(item => {
      //     return item.productid == val;
      //   })[0];
      // } else {
      //   this.currProduct = this.products.filter(item => {
      //     return item.guid == val;
      //   })[0];
      // }
    },
    'specid': function (val) {
        // this.$refs.spname.value = $('#spec').find('option:selected').text();
        this.$refs.spname.value = $('#spec' + this.itemNo).find('option:selected').text();

      this.getPrice();
      if (this.benifitordeduct == 1 && this.settletype == 1 && this.benefittype == 5) {
        this.getCldSettledCount(val);
      }
      if (this.benifitordeduct == 1 && this.settletype == 2 && this.benefittype == 5) {
        this.getCldSettledWeight(val);
      }

    },
    'handselcount': function (val) {
      if (this.specid == '') return;
      let that = this;
      if (this.itemSettledCount + parseFloat(val) > this.items.filter(function (item) {
        return item.spguid == that.specid;
      })[0].handselcount) {
        this.toastText('输入的赠送数量超出未派单大订单赠送数量！');
        this.handselcount = '';
      }
    },
    'handselweight': function (val) {
      if (this.specid == '') return;
      let that = this;
      if (this.itemSettledWeight + parseFloat(val) > this.items.filter(function (item) {
        return item.spguid == that.specid;
      })[0].handselweight) {
        this.toastText('输入的赠送重量超出未派单大订单赠送重量！');
        this.handselweight = '';
      }
    }
  },
  methods: {
    getProducts: function() {
      if (this.sellformid == '') return;
      this.axios({
        method: 'get',
        url: '/sales/Sellform!getProdFromItem.action',
        params: {sellformid: this.sellformid}
      }).then((response) => {
        if (response.data.msg == 'success') {
          let data = response.data;
          this.products = data.products;
        } else {
          this.toastError("系统错误");
        }

      })
    },
    getSpecif: function(productid) {
      this.axios({
        method: 'get',
        url: '/sales/Sellform!getSpecFromItem.action',
        params: {sellformid: this.sellformid, productid: productid}
      }).then((response) => {
        if (response.data.msg == 'success') {
          let data = response.data;
          this.specs = data.specfications;
        } else {
          this.toastError("系统错误");
        }

      })
    },
    getPrice: function() {
      if (this.specid == '') return;
      this.axios({
        method: 'get',
        url: '/sales/Products!getPrice.action',
        params: {speId: this.specid, contractId: this.contract.guid, currency: this.currency, valuation: this.settletype}
      }).then((response) => {
        if (response.data.msg == 'success') {
          this.prices = response.data.products != undefined ? response.data.products : '';
        } else {
          this.toastText('获取价格数据失败')
        }

      })
    },
    getCldSettledCount: function(specid) {
      this.axios({
        method: 'post',
        url: '/sales/Sellitem!getChildsCounts.action',
        params:  {sellformid: this.sellformid, spguid: specid}
      }).then(response => {
        if (response.data.msg == 'success') {
          let counts = response.data.counts != undefined ? response.data.counts : 0;
          this.itemSettledCount = counts;
        } else {
          this.toastText('获取大订单已派单赠送重量失败')
        }
      });
    },
    getCldSettledWeight: function(specid) {
      this.axios({
        method: 'post',
        url: '/sales/Sellitem!getChildsweight.action',
        params:  {sellformid: this.sellformid, spguid: specid}
      }).then(response => {
        if (response.data.msg == 'success') {
          let counts = response.data.counts != undefined ? response.data.counts : 0;
          this.itemSettledWeight = counts;
        } else {
          this.toastText('获取大订单已派单赠送重量失败')
        }
      });
    },
    addItem: function() {
      this.$emit('doAddItem');
    },
    delItem: function() {
      this.$emit('doDelItem');
    }
  }
}
</script>
